package com.dididoctor.patient.Activity.Service;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/15.
 */
public class ServicePresenter extends BasePresenter {

    private ServiceView view;

    public ServicePresenter(Context context, ServiceView view) {
        super(context, view);
        this.view = view;
    }

    public void getService(int pageIndex) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("pageIndex", Util.toString(pageIndex));
        params.put("pageSize", "10");
        params.put("token", Token.getToken());

        BusinessClient.post(ConstantValue.prdlist, params, new MyHttpResponseHandler() {
            private List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            private List<ServiceBean> serviceBeans = new ArrayList<ServiceBean>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    maps = (List<Map<String, Object>>) response.getObject("prdlist");
                    for (Map<String, Object> map : maps) {
                        ServiceBean serviceBean = new ServiceBean();
                        serviceBean.setName(Util.toString(map.get("name")));
                        serviceBean.setPrdId(Util.toString(map.get("prdId")));
                        serviceBean.setDescription(Util.toString(map.get("descr")));
                        serviceBean.setPrize(((Double) map.get("prize")) + "");
                        serviceBean.setPicUrl(Util.toString(map.get("picUrl")));
                        serviceBeans.add(serviceBean);
                    }
                    view.getServiceSucced(serviceBeans);
                } else {
//                    view.showToastMessage(response.getMsg());
//                    view.getServicefail();
                    view.getServiceSucced(serviceBeans);
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getServicefail();
            }
        });

    }
}
